package org.zfm.org.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.zfm.base.BaseDomain;
import org.zfm.base.BaseQuery;
import org.zfm.base.impl.BaseServiceImpl;
import org.zfm.org.domain.Department;
import org.zfm.org.mapper.DepartmentMapper;
import org.zfm.org.service.IDepartmentService;
import org.zfm.util.PageData;

import java.util.Date;
import java.util.List;
import java.util.Objects;


@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    @Override
    public void insert(Department department) {
        department.setCreate_time(new Date());
        departmentMapper.insert(department);
        String path="";
        if (Objects.nonNull(department.getParent().getId())) {
            Department parent = departmentMapper.selectById(department.getParent().getId());
            path = parent.getPath();
        }
        path += "/"+department.getId();
        department.setPath(path);
        departmentMapper.update(department);
    }

    @Override
    public void update(Department department) {
        department.setUpdate_time(new Date());
        String path="";
        if (Objects.nonNull(department.getParent().getId())) {
            Department parent = departmentMapper.selectById(department.getParent().getId());
            path = parent.getPath();
        }
        path += "/"+department.getId();
        department.setPath(path);
        departmentMapper.update(department);
    }
/*
    @Override
    @Transactional
    public void delete(Serializable id) {
        departmentMapper.delete(id);
    }*/

    @Override
    public PageData<Department> queryPageData(BaseQuery query) {
        Long total = departmentMapper.queryTotal(query);
        if(total<1){
            return new PageData<>();
        }
        List<Department> departments = departmentMapper.queryList(query);
        return new PageData<Department>(total,departments);
    }

    @Override
    public List<Department> getTree(BaseDomain department) {
        return departmentMapper.getTree(department);
    }
}
